// MessageTestMain.java

package com.sateh.spaces.examples;

import com.sateh.spaces.SpaceConnection;
import com.sateh.spaces.SpaceConnectionFactory;
import com.sateh.spaces.SpaceException;
import com.sateh.spaces.simple.SimpleSpaceConnectionFactory;

public class MessageTestMain
{
    public static void main(String[] args)
    {
        // Create a space

        SpaceConnectionFactory spaceConnectionFactory = new SimpleSpaceConnectionFactory("vm://MessageTestMain");
        SpaceConnection spaceConnection = spaceConnectionFactory.createConnection();

        // Add 30 messages to it
        
        try {
            for (int i = 0; i < 30; i++) {
                spaceConnection.write(new Message("+31651197975", "This is message #" + i), null, Long.MAX_VALUE);
            }
        } catch (SpaceException exception) {
            System.out.println(exception.getMessage());
        }

        // Create 3 threads with message senders

        for (int i = 0; i < 3; i++) {
            System.out.println("Starting MessageSender thread #" + i);
            MessageSender messageSender = new MessageSender(spaceConnection);
            new Thread(messageSender).start();
        }

        while (true) {
            try {
                Thread.sleep(60 * 1000);
            } catch (InterruptedException e) {
                // Ignored
            }
        }
    }
}
